/*
 * Proja - project management software
 * Copyright (C) 2012  Oleg Kasian
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package org.proja.core.entity.customized;

import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.proja.core.entity.AbstractEntity;
import org.proja.core.entity.enumerations.CustomFieldType;

/**
 *
 * @author oleg
 */
@Entity
@Table(name = "CUTOM_FIELDS")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DTYPE", discriminatorType = DiscriminatorType.STRING)
public abstract class CustomField extends AbstractEntity implements Comparable<CustomField> {

    @Id
    @SequenceGenerator(name = "CustomFieldSeq", sequenceName = "CUSTOM_FIELDS_SEQ")
    @GeneratedValue(generator = "CustomFieldSeq")
    @Column(name = "ID", updatable = false)
    private Long id;

    @Column(name = "NAME")
    private String name;

    @Column(name = "TYPE")
    private CustomFieldType type;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "customField")
    private Set<CFDefaultValue> defaultValues;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "customField")
    private Set<CustomValue> values;

    @Column(name = "POSITION")
    private Integer position;

    @Column(name = "IS_VISIBLE")
    private boolean visible;

    @Column(name = "IS_ACTIVE")
    private boolean active;

    @Column(name = "IS_MANDATORY")
    private boolean mandatory;

    public Long getId() {
        return id;
    }

    public void setId(final Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public CustomFieldType getType() {
        return type;
    }

    public void setType(final CustomFieldType type) {
        this.type = type;
    }

    public Set<CFDefaultValue> getDefaultValues() {
        return defaultValues;
    }

    public void setDefaultValues(final Set<CFDefaultValue> defaultValues) {
        this.defaultValues = defaultValues;
    }

    public Integer getPosition() {
        return position;
    }

    public void setPosition(final Integer position) {
        this.position = position;
    }

    public Set<CustomValue> getValues() {
        return values;
    }

    public void setValues(final Set<CustomValue> values) {
        this.values = values;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(final boolean visible) {
        this.visible = visible;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(final boolean active) {
        this.active = active;
    }

    public boolean isMandatory() {
        return mandatory;
    }

    public void setMandatory(final boolean mandatory) {
        this.mandatory = mandatory;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CustomField other = (CustomField) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    public int compareTo(final CustomField cf) {
        return cf.getPosition().compareTo(position);
    }
}
